package listener;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import model.Layer;
import view.MainFrame;
import controller.LayerController;

public class LayeredPaneListener implements MouseListener, MouseMotionListener
{
    private int m_decalX;
    private int m_decalY;
    private int m_prevX;
    private int m_prevY;
    private int m_mouseX;
    private int m_mouseY;
    private volatile boolean m_mouseDown = false;

    /**
     * use for move the selected layer
     */
    @Override
    public void mouseDragged(MouseEvent e)
    {
        //- MOVE Mode
        if (MainFrame.getInstance().getCurrentAction() == MainFrame.Action.MOVE){
            Layer layerSelected = MainFrame.getInstance().getDrawingPane().getSelectedLayer();
            layerSelected.setLocation(e.getX()-m_decalX, e.getY()-m_decalY);
        }
        //- DRAW Mode
        if (MainFrame.getInstance().getCurrentAction()  == MainFrame.Action.DRAW){
            LayerController.drawPoint(
                    MainFrame.getInstance().getDrawingPane().getSelectedLayer(),
                    MainFrame.getInstance().getColorSelected(),
                    m_prevX, m_prevY, e.getX(), e.getY());
            m_prevX = e.getX();
            m_prevY = e.getY();
        }
        //- BRUSH Mode
        if (MainFrame.getInstance().getCurrentAction()  == MainFrame.Action.BRUSH){
            LayerController.drawBrush(
                    MainFrame.getInstance().getDrawingPane().getSelectedLayer(),
                    MainFrame.getInstance().getColorSelected(), MainFrame.getInstance().getSizeSelected(),
                    m_prevX, m_prevY, e.getX(), e.getY());
            m_prevX = e.getX();
            m_prevY = e.getY();
        }
        //- SPRAY Mode
        if (MainFrame.getInstance().getCurrentAction()  == MainFrame.Action.SPRAY){
            m_mouseX = e.getX();
            m_mouseY = e.getY();
        }
        //- ERASE Mode
        if (MainFrame.getInstance().getCurrentAction() == MainFrame.Action.ERASE){
            LayerController.erase(
                    MainFrame.getInstance().getDrawingPane().getSelectedLayer(),
                    MainFrame.getInstance().getSizeSelected(),
                    e.getX(), e.getY());
        }
    }
    
    
    /**
     * use for move the selected layer
     */
    @Override
    public void mousePressed(MouseEvent e)
    {
        //- MOVE Mode
        if (MainFrame.getInstance().getCurrentAction() == MainFrame.Action.MOVE){
            Layer layerSelected = MainFrame.getInstance().getDrawingPane().getSelectedLayer();
            m_decalX = (int) (e.getX() - layerSelected.getX());
            m_decalY = (int) (e.getY() - layerSelected.getY());
        }
        //- DRAW Mode
        if (MainFrame.getInstance().getCurrentAction()  == MainFrame.Action.DRAW
                || MainFrame.getInstance().getCurrentAction()  == MainFrame.Action.BRUSH){
            m_prevX = e.getX();
            m_prevY = e.getY();
            
        }
        //- ERASE Mode
        if (MainFrame.getInstance().getCurrentAction()  == MainFrame.Action.ERASE){
            LayerController.erase(
                    MainFrame.getInstance().getDrawingPane().getSelectedLayer(),
                    MainFrame.getInstance().getSizeSelected(),
                    e.getX(), e.getY());
        }
        //- SPRAY Mode
        if (MainFrame.getInstance().getCurrentAction()  == MainFrame.Action.SPRAY){
            m_mouseDown = true;
            m_mouseX = e.getX();
            m_mouseY = e.getY();
            new Thread(new Runnable()
            {
                @Override
                public void run()
                {
                    while (m_mouseDown){
                        LayerController.drawSpray(
                                MainFrame.getInstance().getDrawingPane().getSelectedLayer(),
                                MainFrame.getInstance().getColorSelected(), MainFrame.getInstance().getSizeSelected(),
                                m_mouseX, m_mouseY);
                         try
                        {
                            Thread.sleep(30);
                        }
                        catch (InterruptedException e1)
                        {
                            e1.printStackTrace();
                        }
                    }
                }
            }).start();            
        }
    }

    /**
     * use for move the selected layer
     */
    @Override
    public void mouseReleased(MouseEvent e)
    {
        //- SPRAY Mode
        if (MainFrame.getInstance().getCurrentAction() == MainFrame.Action.SPRAY){
            m_mouseDown = false;
        }
    }

    //---NOT USE----

    @Override
    public void mouseMoved(MouseEvent arg0)
    {
    }

    @Override
    public void mouseClicked(MouseEvent e)
    {
    }

    @Override
    public void mouseEntered(MouseEvent e)
    {
    }

    @Override
    public void mouseExited(MouseEvent e)
    {
    }

}
